package com.gack.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.dao.OpinionDao;
import com.gack.business.dao.Opinionnobind;
import com.gack.business.model.Message;
import com.gack.business.model.VideoOpinion;
import com.gack.business.repository.MessageRepository;
import com.gack.business.repository.VideoOpinionRepository;
import com.gack.helper.common.abstractobj.Result;

/**
 * 
* @ClassName: OpinionService 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月6日 下午3:48:18 
* @version V1.0
 */
@Service
public class OpinionService implements OpinionServiceInterface{

	@Autowired
	private OpinionDao opinionDao;
	@Autowired
	private VideoOpinionRepository opinionRepository;
	@Autowired
	private MessageRepository messageRepository;
	@Autowired
	private BackLogServiceInterface backLogService;
	
	@Override
	public Map<String, Object> getOpinions(String startTime, String endTime,String source,Integer currentPage,Integer pageSize) {
		Map<String , Object> map = new HashMap<>();
		List<Opinionnobind> opinions = opinionDao.getOpinions(startTime, endTime,source, currentPage, pageSize);
		long total = opinionDao.getTotalOpinionCount(startTime, endTime,source, currentPage, pageSize);
		map.put("key", "success");
		map.put("opinions", opinions);
		map.put("total", total);
		return map;
	}

	/**
	 * 意见反馈回复
	 */
	@Override
	public Result replyOpinion(String userid,String opinionId, String replytext) {
		Result result = new Result();
		VideoOpinion opinion = opinionRepository.findOne(opinionId);
		if(opinion == null){
			result.setKey("error_noneOpinion");
			result.setMsg("意见反馈信息不存在，反馈回复失败");
			return result;
		}
		opinion.setReplytext(replytext);
		opinion.setStatus(1);
		opinion = opinionRepository.save(opinion);
		//保存意见反馈信息，为用户发送站内信提示意见反馈信息
		Message message = new Message();
		message.setUserid(opinion.getUserid());
		message.setMessage(replytext);
		message.setCreatetime(new Date());
		message.setTitle("反馈回复");
		message.setMesType(0);
		message.setState(2);
		message.setSendWay(2);
		message.setIsRead(0);
		message = messageRepository.save(message);
		String log = "意见反馈回复>>";
		log += " 回复用户id："+opinion.getUserid();
		log += " 回复内容："+opinion.getReplytext();
		backLogService.addBackLog(userid,log);
		result.setKey("success");
		result.setValue(opinion);
		result.setMsg("意见反馈回复成功");
		return result;
	}

	/**
	 * 删除意见反馈
	 */
	@Override
	public Result dealOpinion(String userid,String opinionId) {
		Result result = new Result();
		Integer dealCount = opinionRepository.dealOpinionById(opinionId);
		if(dealCount == 0){
			result.setKey("error_noneOpinion");
			result.setMsg("意见反馈不存在,意见反馈删除失败");
			return result;
		}
		String log = "意见反馈删除>>";
		log += "意见反馈id为："+opinionId;
		backLogService.addBackLog(userid, log);
		result.setKey("success");
		result.setMsg("意见反馈删除成功");
		return result;
	}

	@Override
	public Result getOpinionInfo(String id) {
		Result result = new Result();
		Opinionnobind opinionnobind = opinionDao.getOpinionInfo(id);
		if(opinionnobind == null){
			result.setKey("error");
			result.setMsg("意见反馈不存在");
			return result;
		}
		result.setKey("success");
		result.setValue(opinionnobind);
		return result;
	}

}
